package com.demo.controller;

import com.demo.common.Result;
import com.demo.pojo.dto.ClassListDTO;
import com.demo.pojo.entity.ClassInfo;
import com.demo.service.ClassService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.Valid;
import java.util.Map;

/**
 * Author：潘建冰
 * Package：com.demo.controller
 * Project：demo
 * name：ClassController
 * Date：2025/9/17  10:20
 */

@RestController
@RequestMapping("/class")
@Validated
public class ClassController {

    private final ClassService classService;

    public ClassController(ClassService classService) {
        this.classService = classService;
    }

    /**
     * 根据id查询班级数据
     * @param id 班级id
     * @return Result
     */
    @GetMapping("/select")
    public Result<ClassInfo> select(@RequestParam Integer id){
        return Result.success(classService.selectById(id));
    }

    /**
     * 保存班级数据
     * @param clazz 班级数据
     * @return  Result
     */
    @PostMapping("/save")
    public Result<Void> save(@Valid @RequestBody ClassInfo clazz){
        classService.saveClass(clazz);
        return Result.success();
    }

    /**
     * 修改班级数据
     * @param clazz 班级数据
     * @return  Result
     */
    @PutMapping("/update")
    public Result<Void> update(@Valid @RequestBody ClassInfo clazz){
        classService.updateClass(clazz);
        return Result.success();
    }

    /**
     * 删除班级数据
     * @param id 班级id
     * @return  Result
     */
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam Integer id){
        classService.removeById(id);
        return Result.success();
    }

    /**
     * 分页查询班级数据
     * @param pageNum 页码
     * @param pageSize 每页数据量
     * @param clName 班级名称
     * @param clGrade 年级
     * @param clMajor 专业
     * @return  Result
     */
    @GetMapping("/listPage")
    public Result<Map<String, Object>> listPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(required = false) String clName,
                                @RequestParam(required = false) Integer clGrade,
                                @RequestParam(required = false) String clMajor){
        //封装DTO对象进行传递
        ClassListDTO classListDTO = new ClassListDTO();
        classListDTO.setClName(clName);
        classListDTO.setClGrade(clGrade);
        classListDTO.setClMajor(clMajor);
        classListDTO.setPageNum(pageNum);
        classListDTO.setPageSize(pageSize);
        return Result.success(classService.listPage(classListDTO));
    }

}

